package com.dingwei.lesyndrome.custom;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.dingwei.lesyndrome.R;
import com.dingwei.lesyndrome.base.FailsBean;
import com.dingwei.lesyndrome.base.MyBaseBean;
import com.dingwei.lesyndrome.ui.activity.LoginActivity;
import com.dingwei.lesyndrome.ui.activity.MainActivity;
import com.dingwei.lesyndrome.ui.activity.PersonalCenterActivity;
import com.google.gson.Gson;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.utils.AppManager;
import com.lihao.baselibrary.utils.L;
import com.lihao.baselibrary.utils.T;
import com.lihao.baselibrary.utils.UrlUtils;
import com.lihao.baselibrary.weight.LoadingDialog;

import org.w3c.dom.Text;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;


/**
 * Created by lihao on 2017/8/17.
 */
public abstract class myCallback implements Callback.ProgressCallback<String> {
    private LoadingDialog dialog;
    private Context context;
    private Class c;
    private boolean isFirst = true;

    public myCallback(Context context, Class c, boolean dialogShow) {
        this.context = context;
        this.c = c;
        this.isFirst = dialogShow;
        dialog = new LoadingDialog(context, R.style.DialogStyle);
    }
    public myCallback(Context context, Class c, boolean dialogShow,RequestParams rp) {
        this.context = context;
        this.c = c;
        this.isFirst = dialogShow;
        dialog = new LoadingDialog(context, R.style.DialogStyle);
        L.e(UrlUtils.getUrlParams(rp));
    }

    public myCallback(Context context, Class c) {
        this.context = context;
        this.c = c;
        this.isFirst = true;
        dialog = new LoadingDialog(context, R.style.DialogStyle);
    }
    public myCallback(Context context, Class c,RequestParams rp) {
        this.context = context;
        this.c = c;
        this.isFirst = true;
        dialog = new LoadingDialog(context, R.style.DialogStyle);
        L.e(UrlUtils.getUrlParams(rp));
    }

    @Override
    public void onWaiting() {

    }

    @Override
    public void onStarted() {
        if (isFirst) {
            dialog.show();
        }
    }

    @Override
    public void onLoading(long l, long l1, boolean b) {

    }

    @Override
    public void onSuccess(String json) {
        String s = JsonUtils.replaceJsonString(json);

        L.e("接口请求的数据--------> "+s);
        MyBaseBean bean = (MyBaseBean) new Gson().fromJson(s, c);
        if (bean.status == 1) {
            back(bean, s);
        } else {
            T.showShort(context, bean.message);
            FailsBean failsBean = new Gson().fromJson(s,FailsBean.class);
            if (failsBean.status==0){
                if (failsBean.data!=null&&!TextUtils.isEmpty(failsBean.data.error_code)&&failsBean.data.error_code.equals("00001")){
                    BaseActivity.value.setIsLogin(false);
                    BaseActivity.value.setToken("");
                    BaseActivity.value.setHeadImg("");
                    BaseActivity.value.setUserName("");
                    BaseActivity.value.setPhone("");
                    Intent intent = new Intent(BaseActivity.context, LoginActivity.class);
                    BaseActivity.context.startActivity(intent);
                    AppManager.getAppManager().finishActivity();
                    AppManager.getAppManager().finishActivity(PersonalCenterActivity.class);
                }
            }

            fail(failsBean, s);
        }

    }

    @Override
    public void onError(Throwable throwable, boolean b) {
        T.showShort(BaseActivity.context, com.lihao.baselibrary.R.string.msg_net_error);
        fail(new FailsBean(), BaseActivity.context.getResources().getString(com.lihao.baselibrary.R.string.msg_net_error));
        L.e("---------------->"+throwable.getMessage());
    }

    @Override
    public void onCancelled(CancelledException e) {
    }

    @Override
    public void onFinished() {
        dialog.dismiss();

    }

    public abstract void back(MyBaseBean baseBean, String result);

    public abstract void fail(FailsBean baseBean, String result);

}
